<?php

namespace common\domain\components\actions;

use common\domain\base\ApiResponse;
use common\domain\helper\Request;
use Yii;
use yii\rest\Action;
use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException;

class DeleteAction extends Action
{
    use ApiResponse;

    /**
     * @return array
     * @throws ServerErrorHttpException
     */
    public function run(): array
    {
        $id = Request::getRequest('id');
        try {
            $model = $this->findModel($id);
        } catch (NotFoundHttpException $e) {
            return  $this->responseError('操作失败!，数据不存在！');
        }

        if ($this->checkAccess) {
            call_user_func($this->checkAccess, $this->id, $model);
        }

        if ($model->delete() === false) {
            throw new ServerErrorHttpException('未知原因，删除失败！');
        }

        return $this->responseSuccess();
    }
}
